package com.zjsru.oneDay202205;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: likew
 * @Date: 2022/5/13
 * 一次编辑
 * 字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。
 * 输入:
 * first = "pale"
 * second = "ple"
 * 输出: True
 */
public class oneEditAway {
    public boolean oneEditAway(String first, String second) {
        int n = first.length(), m = second.length();
        if (Math.abs(n - m) > 1) {
            return false;
        }
        if(n > m) return oneEditAway(second, first);
        int i = 0, j = 0, cnt = 0;
        while(i < n && j < m && cnt <= 1){
            if(first.charAt(i) == second.charAt(j)){
                i++;
                j++;
            }else {
                if(n == m){
                    j++;
                    i++;
                    cnt++;
                }else {
                    j++;
                    cnt++;
                }
            }
        }
        return cnt <= 1;
    }
    
    public static void main(String[] args) {
        oneEditAway oneEditAway = new oneEditAway();
        String first = "pale";
        String second = "ple";
        System.out.println(oneEditAway.oneEditAway(first, second));
    }
}
